from django.http import HttpResponse
from django.shortcuts import render, redirect

from app.utils import FileUpload
from session import settings


def check_login(func):
    def inner(*args, **kwargs):
        if args[0].session.get("user_session"):
            return func(*args, **kwargs)
        else:
            return redirect("user/login")

    return inner


@check_login
def home(request):
    # 获取session
    user_session = request.session.get("user_session")
    print("user_session----{}".format(user_session))
    return render(request, "home/home.html")


# 文件上传---form表单
def uploadfile(request):
    if request.method == 'GET':
        return render(request, "home/upfile.html")

    elif request.method == 'POST':
        # 使用自定义文件上传类
        fobj = request.FILES.get('photo')
        path = settings.MDEIA_ROOT
        fp = FileUpload(fobj)   # fobj传入的是一个文件上传对象
        if fp.upload(path):
            return HttpResponse("文件上传成功")
